﻿using System;
using System.Collections.Generic;
using Autodesk.Revit.DB;
using YArchitech.Revit;
using YJKArchUtils.Utils;
using YJKRevitDimensionText.OneKeyPlanDim.Utility;

namespace YJKRevitDimensionText.OneKeyPlanDim.DimData
{
	public class CarBasePlanarFace
	{
		public CarBasePlanarFace(Face face)
		{
			this.m_RuledFaceOrPlanarFace = face;
		}

		public double FloorThickness
		{
			get
			{
				return this.m_dFloorThickness;
			}
			set
			{
				this.m_dFloorThickness = value;
			}
		}

		public Face BasePlanarFace
		{
			get
			{
				return this.m_RuledFaceOrPlanarFace;
			}
		}

		public XYZ SlopeDir
		{
			get
			{
				return this.m_SlopeDir;
			}
		}

		public string SlopeValue
		{
			get
			{
				return this.m_strSlopeValue;
			}
		}

		public XYZ OutPoint
		{
			get
			{
				return this.m_outPoint;
			}
		}

		public XYZ InPoint
		{
			get
			{
				return this.m_inPoint;
			}
		}

		public bool IsSlopePlanarFace()
		{
			return !Geometry.IsEqual(this.CalSlopeValue(), 0.0);
		}

		private double CalSlopeValue()
		{
			XYZ xyz = XYZ.Zero;
			XYZ xyz2 = XYZ.Zero;
			double num = double.MinValue;
			double num2 = double.MaxValue;
			foreach (Curve curve in GeometryHelper.GetAllEdgeList(this.m_RuledFaceOrPlanarFace))
			{
				XYZ xyz3 = (curve.GetEndPoint(0) + curve.GetEndPoint(1)) / 2.0;
				if (Geometry.LessThan(num, xyz3.Z))
				{
					xyz = xyz3;
					num = xyz3.Z;
				}
				if (Geometry.LessThan(xyz3.Z, num2))
				{
					xyz2 = xyz3;
					num2 = xyz3.Z;
				}
			}
			XYZ xyz4 = new XYZ(xyz.X, xyz.Y, 0.0);
			XYZ xyz5 = new XYZ(xyz2.X, xyz2.Y, 0.0);
			double num3 = xyz4.DistanceTo(xyz5);
			double num4 = xyz.Z - xyz2.Z;
			if (num4 != 0.0 && num3 != 0.0)
			{
				double num5 = num4 / num3;
				this.m_strSlopeValue = (num5 * 100.0).ToString("f0") + "%";
				return num5;
			}
			return 0.0;
		}

		public bool IsCarBasePlanarFace(Curve trackCv)
		{
			XYZ xyz = RevitDocumentExtension.GetEndPoint(trackCv, 0);
			XYZ xyz2 = RevitDocumentExtension.GetEndPoint(trackCv, 1);
			if (Geometry.IsEqual(xyz.Z, xyz2.Z))
			{
				return false;
			}
			List<Curve> allEdgeList = GeometryHelper.GetAllEdgeList(this.m_RuledFaceOrPlanarFace);
			using (List<Curve>.Enumerator enumerator = allEdgeList.GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					if (Geometry.IsEqual(enumerator.Current.Length, this.m_dFloorThickness, 0.01))
					{
						return false;
					}
				}
			}
			if (Geometry.LessThan(xyz2.Z, xyz.Z))
			{
				XYZ xyz3 = xyz2;
				xyz2 = xyz;
				xyz = xyz3;
			}
			bool result = false;
			foreach (Curve curve in allEdgeList)
			{
				XYZ xyz4 = RevitDocumentExtension.GetEndPoint(curve, 0);
				XYZ xyz5 = RevitDocumentExtension.GetEndPoint(curve, 1);
				if (Geometry.LessThan(xyz5.Z, xyz4.Z))
				{
					XYZ xyz6 = xyz5;
					xyz5 = xyz4;
					xyz4 = xyz6;
				}
				if (xyz.IsAlmostEqualTo(xyz4, 0.0001) && xyz2.IsAlmostEqualTo(xyz5, 0.0001))
				{
					result = true;
				}
				else if (Geometry.IsEqual(xyz.Z, xyz4.Z) && Geometry.IsEqual(xyz5.Z, xyz4.Z))
				{
					if (xyz.IsAlmostEqualTo(xyz4))
					{
						this.m_StartLine = Line.CreateBound(xyz4, xyz5);
					}
					else
					{
						this.m_StartLine = Line.CreateBound(xyz5, xyz4);
					}
					this.m_outPoint = this.m_StartLine.GetEndPoint(1);
					this.m_inPoint = this.m_StartLine.GetEndPoint(0);
				}
			}
			if (this.m_StartLine != null)
			{
				this.m_SlopeDir = XYZ.BasisZ.CrossProduct(this.m_StartLine.GetEndPoint(1) - this.m_StartLine.GetEndPoint(0)).Normalize();
				double num = xyz2.DistanceTo(xyz);
				XYZ xyz7 = xyz + this.m_SlopeDir * Common.MMToFeet(100.0);
				if (!Geometry.LessThan(xyz2.DistanceTo(xyz7), num))
				{
					this.m_SlopeDir = -this.m_SlopeDir;
				}
			}
			return result;
		}

		private Face m_RuledFaceOrPlanarFace;

		private Line m_StartLine;

		private XYZ m_outPoint = XYZ.Zero;

		private XYZ m_inPoint = XYZ.Zero;

		private string m_strSlopeValue = string.Empty;

		private XYZ m_SlopeDir;

		private double m_dFloorThickness;
	}
}
